/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.wallet.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.AES;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.wallet.bo.WalletStatisticsBO;
import com.pig4cloud.pigx.wallet.configuration.SendServiceCharge;
import com.pig4cloud.pigx.wallet.constant.WalletConstant;
import com.pig4cloud.pigx.wallet.constant.WalletStatusEnum;
import com.pig4cloud.pigx.wallet.entity.Account;
import com.pig4cloud.pigx.wallet.entity.Currency;
import com.pig4cloud.pigx.wallet.entity.ImputationRecord;
import com.pig4cloud.pigx.wallet.entity.RechargeRecord;
import com.pig4cloud.pigx.wallet.service.AccountService;
import com.pig4cloud.pigx.wallet.service.CurrencyService;
import com.pig4cloud.pigx.wallet.service.ImputationRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import okhttp3.OkHttpClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.core.contract.Trc20Contract;
import org.web3j.abi.FunctionEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthCall;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.utils.Numeric;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 归集列表
 *
 * @author
 * @date 2022-08-16 15:27:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/imputationrecord")
@Api(value = "imputationrecord", tags = "归集列表管理")
public class ImputationRecordController {

	private final ImputationRecordService imputationRecordService;


	/**
	 * 分页查询
	 *
	 * @param page             分页对象
	 * @param imputationRecord 归集列表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('wallet_imputationrecord_view')")
	public R<WalletStatisticsBO<ImputationRecord>> getImputationRecordPage(Page<ImputationRecord> page, ImputationRecord imputationRecord) {
		QueryWrapper<ImputationRecord> wrapper = new QueryWrapper<ImputationRecord>() {{
			if (imputationRecord.getUserId() != null) {
				eq("user_id", imputationRecord.getUserId());
			}
			if (StrUtil.isNotBlank(imputationRecord.getCurrency())) {
				eq("currency", imputationRecord.getCurrency());
			}
			if (StrUtil.isNotBlank(imputationRecord.getMainNetwork())) {
				eq("main_net_work", imputationRecord.getMainNetwork());
			}
			if (StrUtil.isNotBlank(imputationRecord.getState())) {
				eq("state", imputationRecord.getState());
			}
			if (imputationRecord.getStartTime() != null) {
				ge("create_time", imputationRecord.getStartTime());
				le("create_time", imputationRecord.getStartTime().plusDays(1));
			}
			orderByDesc("create_time");
		}};
		Page<ImputationRecord> pageList = imputationRecordService.page(page, wrapper);
		WalletStatisticsBO<ImputationRecord> bo = new WalletStatisticsBO<>();
		ImputationRecord one = imputationRecordService.getOne(wrapper.select("sum(quantity) 'quantity'"));
		bo.setPage(pageList);
		bo.setStatistics(one);
		return R.ok(bo);
	}


	/**
	 * 通过id查询归集列表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('wallet_imputationrecord_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(imputationRecordService.getById(id));
	}

	/**
	 * 新增归集列表
	 *
	 * @param imputationRecord 归集列表
	 * @return R
	 */
	@ApiOperation(value = "新增归集列表", notes = "新增归集列表")
	@SysLog("新增归集列表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('wallet_imputationrecord_add')")
	public R save(@RequestBody ImputationRecord imputationRecord) {
		return R.ok(imputationRecordService.save(imputationRecord));
	}

	/**
	 * 修改归集列表
	 *
	 * @param imputationRecord 归集列表
	 * @return R
	 */
	@ApiOperation(value = "修改归集列表", notes = "修改归集列表")
	@SysLog("修改归集列表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('wallet_imputationrecord_edit')")
	public R updateById(@RequestBody ImputationRecord imputationRecord) {
		return R.ok(imputationRecordService.updateById(imputationRecord));
	}

	/**
	 * 通过id删除归集列表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除归集列表", notes = "通过id删除归集列表")
	@SysLog("通过id删除归集列表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('wallet_imputationrecord_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(imputationRecordService.removeById(id));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param imputationRecord 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('wallet_imputationrecord_export')")
	public List<ImputationRecord> export(ImputationRecord imputationRecord) {
		return imputationRecordService.list(Wrappers.query(imputationRecord));
	}

	/**
	 * 创建归集
	 */
	@Inner(value = false)
	@ApiOperation(value = "创建归集", notes = "创建归集")
	@PostMapping("/create")
	public R create(@RequestBody ImputationRecord imputationRecord) {
		imputationRecordService.create(imputationRecord);
		return R.ok("正在进行归集");
	}
}
